const RecomBookModel = require('../db/models/recomBook');

class RecomCourseService {
    //爬取推荐书本数据
    async addRecomBook (data) {
        const bid = data.bid;

        const result = await RecomBookModel.findOne({
            where: { bid }
        })

        if (result) {
            return await RecomBookModel.update(data, {
                where: { bid }
            })
        } else {
            return await RecomBookModel.create(data);
        }
    }

    //获取推荐书本数据
    async getRecomBookData () {
        return await RecomBookModel.findAll({
            attributes: {
                exclude: ['href','description', 'createdAt', 'updatedAt']
            }
        });
    }

    //改变推荐书本上下架状态
    async changeRecomBookStatus (id, status) {
        const ret = await RecomBookModel.update({ status }, { 
            where: { bid: id } 
        });

        return ret[0];
    }
}

module.exports = new RecomCourseService();